package customerEnter.Frame.JDBCDao.SqlProcessing.SqlServer;

import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;

import customerEnter.Frame.DaoDataTransmission.DaoFramUpdateOrAddMap;
import customerEnter.Frame.DaoDataTransmission.DaoFrameDMLBaseTransmission;
import customerEnter.Frame.DaoDataTransmission.DaoFramePameTransmission;
import customerEnter.Frame.DaoDataTransmission.PagingParametersResult;
import customerEnter.Frame.DaoDataTransmission.SqlPrecompileModel;
import customerEnter.Frame.DaoDataTransmission.SqlPrecompileParameter;
import customerEnter.Frame.Enum.T_SQLConditionEnum;
import customerEnter.Frame.JDBCDao.Interface.DaoFrameSqlProcessingInterface;
import customerEnter.Frame.JDBCDao.Interface.SqlPrecompileInterface;
import customerEnter.Frame.JDBCDao.Interface.SqlPrecompileParameterInterface;
import customerEnter.Frame.JDBCDao.util.DaoFrameConntroller;
import customerEnter.Frame.Utils.Log4j;
import customerEnter.Frame.Utils.javaReflexClass;
import customerEnter.tool.WhetherToEmpty;

/**
 * 类描述：處理 修改的語句
 * 
 * 类名称：DaoFrameSqlProcessingInsertImpl
 * 
 * 创建人：dy
 * 
 * 创建时间：2017-7-12 下午3:50:39
 * 
 */
public class DaoFrameSqlProcessingUpdateBatchImpl implements DaoFrameSqlProcessingInterface<String> {

	@SuppressWarnings("rawtypes")
	@Override
	public <T, K> String SqlProcessing(DaoFrameDMLBaseTransmission daoFrameDMLBaseTransmission,
			DaoFramePameTransmission<T, K> daoFramePameTransmission) {
		List<Object[]> objects = new ArrayList<Object[]>();
		try {

			if (daoFrameDMLBaseTransmission.isIsPreparedStatementParameter()) {
				objects = daoFrameDMLBaseTransmission.getListObjects();

				daoFramePameTransmission.SQL = DaoFrameConntroller.SqlManage(daoFrameDMLBaseTransmission.getDataBase(),
						daoFrameDMLBaseTransmission.getDML(), "", "", daoFrameDMLBaseTransmission.getTableName(),
						T_SQLConditionEnum.Where.getValue() + daoFrameDMLBaseTransmission.getWhere());
			} else {
				objects = setUpdateMappingSql(daoFrameDMLBaseTransmission.getDataBase(),
						daoFrameDMLBaseTransmission.getTableName(), daoFrameDMLBaseTransmission.getValues(),
						daoFrameDMLBaseTransmission.getWhere(), daoFramePameTransmission);
			}
			daoFramePameTransmission.setSqlPrecompileParameterInterface(InitlaztationListObjects(objects,
					new SqlPrecompileParameter()));

		} catch (Exception e) {
			Log4j.setLog4j(getClass());
			Log4j.Log4JOutError(e.getMessage());
		}
		daoFramePameTransmission.setPagingParametersResult(new PagingParametersResult());
		return daoFramePameTransmission.SQL;
	}

	/**
	 * 修改數據的方法
	 * 
	 * @param <T>
	 * @param <K>
	 * 
	 * @throws ReflectiveOperationException
	 * @throws IllegalArgumentException
	 */
	public <T, K> List<Object[]> setUpdateMappingSql(String DataBase, String TableName, List<Object> t, String where,
			DaoFramePameTransmission<T, K> daoFramePameTransmission) throws Exception {
		List<Object[]> objects = new ArrayList<Object[]>();
		for (Object object : t) {
			List<Object> list = new ArrayList<Object>();
			String string = "";// 獲取全部屬性的類
			DaoFramUpdateOrAddMap daoFramUpdateOrAddMap = javaReflexClass.getObjectNamesAndValuesUpdate(object);
			for (Entry<String, Object> entry : daoFramUpdateOrAddMap.getData().entrySet()) {
				if (!WhetherToEmpty.StringWhetherToEmpty(string)) {
					string += ",";
				}
				string += entry.getKey() + "=" + "?";
				list.add(entry.getValue());
			}
			if (WhetherToEmpty.ObjectWhetherToEmpty(daoFramePameTransmission.SQL)) {
				daoFramePameTransmission.SQL = DaoFrameConntroller.SqlManage(DataBase, " update ", TableName, " set ",
						string, " where 1=1  " + where);
			}
			objects.add(list.toArray());
		}
		return objects;
	}

	public SqlPrecompileParameterInterface InitlaztationListObjects(List<Object[]> objects,
			SqlPrecompileInterface sqlPrecompileInterface) {
		for (Object[] objects2 : objects) {

			int i = 1;
			for (Object object : objects2) {
				sqlPrecompileInterface.AddNumberValue(i, object);

				i++;
			}
			sqlPrecompileInterface.AddBatchSqlParameterModel();
		}
		return (SqlPrecompileParameterInterface) sqlPrecompileInterface;
	}

	public SqlPrecompileParameterInterface InitlaztationdSqlPrecompileModel(List<List<SqlPrecompileModel>> list,
			SqlPrecompileInterface sqlPrecompileInterface) {
		for (List<SqlPrecompileModel> list2 : list) {
			for (SqlPrecompileModel sqlPrecompileModel : list2) {
				sqlPrecompileInterface.AddNumberValue(sqlPrecompileModel.getColumnName(), sqlPrecompileModel
						.getNumber().intValue(), sqlPrecompileModel.getObject());
			}
			sqlPrecompileInterface.AddBatchSqlParameterModel();
		}
		return (SqlPrecompileParameterInterface) sqlPrecompileInterface;
	}
}
